import constantRoutes from "./constant"
import menuRoutes from "./meun"
import nprogress from "nprogress"
import "nprogress/nprogress.css"
import { store } from "@/store"
import { createRouter, createWebHashHistory } from "vue-router"
import type { RouteRecordRaw } from "vue-router"
import type { App } from "vue"

//nprogress 配置
nprogress.configure({
  easing: "ease",
  speed: 500,
  trickleSpeed: 200,
  showSpinner: false,
})

const router = createRouter({
  history: createWebHashHistory(),
  routes: [...constantRoutes, ...menuRoutes] as Array<RouteRecordRaw>,
})

//路由前置守卫
router.beforeEach((to, from, next) => {
  document.title = to.meta.title
  nprogress.start()
  store.commit("admin/pushRouterStack", {
    canItBeClosed: true,
    title: to.meta.title,
    path: to.path,
  })
  next()
})
//路由后置守卫
router.afterEach(() => {
  nprogress.done()
})

export default function setupRouter(app: App) {
  app.use(router)
}
